package com.xinghou.platform.lucene.main.impl;

import com.xinghou.platform.lucene.main.SharedCardLuceneService;
import com.xinghou.platform.lucene.main.bean.SharedCardSolr;
import com.xinghou.platform.lucene.main.bean.SolrServerFactory;
import com.xinghou.platform.lucene.main.util.SolrJOper;
import com.xinghou.platform.lucene.main.util.SolrQuery;
import com.xinghou.platform.model.main.SharedCard;
import com.xinghou.platform.model.main.SharedCardQueryParam;
import com.xinghou.platform.page.Pagination;
import com.xinghou.platform.util.SolrUtil;
import com.xinghou.platform.util.StringUtils;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrServerException;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by qingshengzheng on 16/4/26.
 */
@Service("sharedCardService")
public class ShareCardSolrServiceImpl implements SharedCardLuceneService {
    private SolrJOper<SharedCardSolr> solrOper = new SolrJOper<SharedCardSolr>();
    private SolrServerFactory solrServerFactory = SolrServerFactory.instance();
    @Override
    public List<SharedCard> getSearchSharedCards(String key, int fromno, int pagesize) throws IOException {
        SolrClient server = solrServerFactory.getShareCardServer();
        HashMap map=new HashMap();
        if(!StringUtils.isEmpty(key)&&!"null".equals(key)) {
            map.put("cardcontent", "*" + key + "*");
            map.put("storename", "*" + key + "*");
        }
        String searchKey= SolrUtil.getOrSearchKey(map);
        Pagination<SharedCardSolr> values = solrOper.queryBean(server, searchKey, fromno, pagesize, SharedCardSolr.class);
        if (values==null||values.getValues() == null || values.getValues().isEmpty()) return null;
        List<SharedCard> sharedCards = new ArrayList<SharedCard>();
        List<SharedCardSolr> solrs = values.getValues();
        for (int i = 0; i < solrs.size(); i++) {
            SharedCard temp = solrs.get(i).toSharedCard();
            sharedCards.add(temp);
        }
        return sharedCards;
    }

    @Override
    public List<SharedCard> getSearchSharedCardsbyGeo(String key, int fromno, int pagesize, double lat, double lng) throws IOException {
        List<SharedCard> cards=getSearchSharedCardsbyGeo(key,fromno,pagesize,1000000000d,lat,lng);
        return cards;

    }

    @Override
    public List<SharedCard> getSearchSharedCardsbyGeo(String key, int fromno, int pagesize,  Double distince,double lat, double lng) throws IOException {
        HashMap map=new HashMap();
        if(!StringUtils.isEmpty(key)&&!"null".equals(key)) {
            map.put("cardcontent", "*" + key + "*");
            map.put("storename", "*" + key + "*");
        }
        String searchKey= SolrUtil.getOrSearchKey(map);
        SolrClient server = solrServerFactory.getShareCardServer();
        Pagination<SharedCardSolr> values = solrOper.queryLbsBean(server, searchKey,distince, "position",lat+","+lng, fromno, pagesize,  SharedCardSolr.class);
        if (values==null||values.getValues() == null || values.getValues().isEmpty()) return null;
        List<SharedCard> sharedCards = new ArrayList<SharedCard>();
        List<SharedCardSolr> solrs = values.getValues();
        for (int i = 0; i < solrs.size(); i++) {
            SharedCard temp = solrs.get(i).toSharedCard();
            sharedCards.add(temp);
        }
        return sharedCards;
    }

    @Override
    public int saveSharedCard(SharedCard sharedCard) {
        SolrClient server = solrServerFactory.getShareCardServer();
        solrOper.addBean(server, SharedCardSolr.fromSharedCard(sharedCard));
        return 0;
    }

    @Override
    public List<SharedCard> queryBySharedCardParam(SharedCardQueryParam param, int fromno, int pagesize, Double distince, double lon, double lat) {
        SolrClient server = solrServerFactory.getShareCardServer();
        SolrQuery solrQuery=new SolrQuery();
        if(!StringUtils.isEmpty(param.getStorename()))
            solrQuery.like(SolrQuery.OPER_AND,"snameoraddress",param.getStorename());
        if(param.getUserarea()!=null){
            String[] areas=param.getUserarea().split("-");
            solrQuery.in(SolrQuery.OPER_AND,"usearea", areas);
        }
        solrQuery.equal(SolrQuery.OPER_AND,"status","1");
        if(param.mindiscount!=null)
            solrQuery.between(SolrQuery.OPER_AND,"discount",param.mindiscount+"",param.maxdiscount+"");


        String key=solrQuery.toString();
        Pagination<SharedCardSolr> values = solrOper.queryLbsBean(server, key,distince, "position",lat+","+lon, fromno, pagesize,  SharedCardSolr.class);
        if (values==null||values.getValues() == null || values.getValues().isEmpty()) return null;
        List<SharedCard> sharedCards = new ArrayList<SharedCard>();
        List<SharedCardSolr> solrs = values.getValues();
        for (int i = 0; i < solrs.size(); i++) {
            SharedCard temp = solrs.get(i).toSharedCard();
            sharedCards.add(temp);
        }
        return sharedCards;
    }

    @Override
    public int updateSharedCard(SharedCard sharedCard) {
        SolrClient server = solrServerFactory.getShareCardServer();
        try {
            server.deleteById(sharedCard.getCardid() + "");
            solrOper.addBean(server, SharedCardSolr.fromSharedCard(sharedCard));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int deleteShareCard(Integer cardid) {
        SolrClient server = solrServerFactory.getShareCardServer();
        try {
            server.deleteById(cardid + "");
        } catch (Exception e) {
            e.printStackTrace();

        }
        return 0;
    }

}
